package com.example.gateway8000.myfilter;

import com.zhy.utils.ResultTool;
import lombok.SneakyThrows;
import org.apache.http.HttpResponse;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.net.URI;

//@Order(-1)
//@Component
public class MyFilter implements GlobalFilter , Ordered {
    @SneakyThrows
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
//        URI uri = request.getURI();
//        System.out.println(uri);
//        System.out.println(uri.toString());
        HttpHeaders headers = request.getHeaders();
        String token = headers.getFirst("token");



        if (token!=null){
            System.out.println(token);
            return chain.filter(exchange);
        }

        exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
        return exchange.getResponse().setComplete();
    }

    @Override
    public int getOrder() {
        return 200;
    }
}
